/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.match.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Map;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.SpriteSheetCache;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GUIDStorage;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.AbilityAttackAction;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.ActionCoordinateInfo;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.ActionMediator;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.ActionSpellInfo;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.CharacterAttackAction;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.GenericPawn;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.AbilityStats;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.AbilityValue;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Massimo Canonaco
 *
 */
public class AttackInformationPanel016 extends JPanel 
{
	static public final String NAME = "AttackInformationPanel";
	
	/** */
	private CharacterStatusPanel016 container;
	
	/** */
	private JList<JListAttackData> attacks;
	
	/** */
	private DefaultListModel<JListAttackData>attacksModel;
	
	/** */
	private ImageIcon AreaOfEffect_On;
	
	/** */
	private ImageIcon AreaOfEffect_Off;
	
//	/** */
//	private ImageIcon cost;
	
	/** */
	private ImageIcon friendly_On;
	
	private ImageIcon selfOnly;
	
	/** */
	private ImageIcon friendly_Off;
	
//	/** */
//	private Icon testIcon1;
//	private Icon testIcon2;
	
	public AttackInformationPanel016(final CharacterStatusPanel016 containerPanel, Map<String,String> settings, SpriteSheetCache imgCache) 
	{
		super();
		container = containerPanel;
		OverallGUILayoutFactory gui = OverallGUILayoutFactory.getInstance();
		attacksModel = new DefaultListModel<>();
		JListAttackData normalAttack = new JListAttackData(container)
		{
			@Override
			public void action()
			{
				container.getCicp().setReady(true);
				GenericPawn actualActor = container.getCicp().getActualCharacter();
//				ActionMediator mediator = container.getCicp().getMatchManagerMonitor().getCurrentPlayer().getActions();
				ActionMediator mediator = container.getCicp().getMatchManagerMonitor().getClientPlayer().getActions();
//				mediator.getCurrentAction().reset();
				mediator.clearInformations();
				mediator.setAction(GUIDStorage.CHARACTER_ATTACK_ACTION);
//				mediator.getCurrentAction().reset();
				mediator.addActionInfo(new ActionCoordinateInfo(actualActor.getRow(), actualActor.getColumn()));
			}
		};
		
		normalAttack.displayName = "Attacco Base";
		normalAttack.setCost(Integer.toString(CharacterAttackAction.COST));
		attacksModel.addElement(normalAttack);
		addDatasToModel();
		attacks = new JList<>(attacksModel);
		try
		{
			friendly_On = new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.allied.onicon.path"))));
			friendly_Off = new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.allied.officon.path"))));
			AreaOfEffect_On = new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.aoe.onicon.path"))));
			AreaOfEffect_Off = new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.aoe.officon.path"))));
			selfOnly = new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.allied.selfonlyicon.path"))));
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		attacks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		attacks.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		
//		attacks.setOpaque(false);
		Color overallBackground = gui.getParsedColor(settings.get("characterinformationpanel.ability.background"));
		attacks.setBackground(overallBackground);
//		attacks.setSelectionBackground(Color.black);
//		attacks.set
//		attacks.setLayoutOrientation(JList.VERTICAL_WRAP);
//		attacks.setselectionm
		JLAttackDataRender rnd = new JLAttackDataRender(settings,gui.getParsedColor(settings.get("characterinformationpanel.ability.selected")),overallBackground, imgCache);
		attacks.setCellRenderer(rnd);
		attacks.setVisibleRowCount(-1);
//		rnd.set
		attacks.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent arg0) 
			{
//				arg0.get
				JListAttackData selected = attacks.getSelectedValue();
				if(selected != null && !arg0.getValueIsAdjusting())
				{
					selected.action();
//					attacks.getSelectedValue().action();
					//				attacksModel.get(arg0.getFirstIndex()).action();
//					attacks.clearSelection();
				}
			}
		});
//		attacks.setFixedCellWidth(150);
//		attacks.setFixedCellHeight(55);
		attacks.setPreferredSize(new Dimension(600,180));
//		JButton back = new JButton("Indietro");
		JButton back = gui.createButtonUniState(Integer.parseInt(settings.get("characterinformationpanel.button.text.size")), "Indietro", Integer.parseInt(settings.get("characterinformationpanel.button.width")), Integer.parseInt(settings.get("characterinformationpanel.button.height")), OverallGUILayoutFactory.BUTTON_STATE_ACTIVE);
		back.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				
				ActionMediator mediator = container.getCicp().getMatchManagerMonitor().getClientPlayer().getActions();
				mediator.setNoAction();
				container.getCicp().setReady(false);
//				attacks.clearSelection();
				container.showBasePanel();
				reset();
			}
		});
		
		JScrollPane scroll = new JScrollPane(attacks,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//		scroll.add(attacks);
		
		scroll.setPreferredSize(new Dimension(800,160));
//		setOpaque(true);
		this.add(scroll);
		
		
//		scroll.setBackground(gui.getParsedColor(settings.get("characterinformationpanel.ability.background")));
		scroll.setBackground(Color.green);
		scroll.setOpaque(false);
//		this.add(attacks);
//		attacks.setMinimumSize(new Dimension(750,180));
		this.add(back);
//		setOpaque(false);
//		gui.getParsedColor(settings.get("characterinformationpanel.ability.background"))
		setBackground(gui.getParsedColor(settings.get("characterinformationpanel.attack.background")));
//		rnd.setOpaque(false);

//		this.setPreferredSize(new Dimension(800,180));
	}
	
	void updateLabels(Character newPawn)
	{
		if(newPawn != null)
		{
			LinkedList<AbilityStats> stats = newPawn.getStats().getAbilities();
			int size =  attacksModel.getSize();
			JListAttackData data = null;
			AbilityValue dataAbilityValue = null;
			
			for(int index = 1 ; index < size; index++)
			{
				data = attacksModel.get(index);
				dataAbilityValue = stats.get(index-1).getAbilityValue();
//				attacksModel.get(index).displayName = stats[index-1].getAbilityValue().getName()+" "+stats[index-1].getAbilityValue().isFriendly()+" "+ stats[index-1].getAbilityValue().isAreaOfEffect();
				data.displayName = " "+dataAbilityValue.getName()+" ";
				data.setCost(" "+Integer.toString((int) dataAbilityValue.getCost())+" ");
				if(dataAbilityValue.isAreaOfEffect())
				{
					data.setAoEIcon(AreaOfEffect_On);
				}
				else
				{
					data.setAoEIcon(AreaOfEffect_Off);
				}
				
				if(!dataAbilityValue.isFriendly())
				{
					data.setFriendlyIcon(friendly_Off);
				}
				else if(dataAbilityValue.isSelfOnly())
				{
					data.setFriendlyIcon(selfOnly);
				}
				else data.setFriendlyIcon(friendly_On);
			}
			
		}
	}
	
	private void addDatasToModel()
	{
		JListAttackData tempData = null;
		// FIXED 6 SPELLS + 1 BASIC ATTACK
		int attacksSize = ActionSpellInfo.SIXTH_SPELL;

			for(int index = 0; index <= attacksSize; index++)
			{
				tempData = new JListAttackData(container);
				tempData.attackID = index;
				attacksModel.addElement(tempData);
				tempData.p = this;
			}
//		}
	}

	public void reset() 
	{
		attacks.clearSelection();
	}
	
	
	
	
}
